/*
 * HttpPostExample.h
 *
 * Taken as is from Bada IDE 2.0.4 Examples:
 * bada\2.0.4\Examples\Communication\inc\Http
 *
 */

#include "HttpPostExample.h"

using namespace Osp::Base;
using namespace Osp::Net::Http;

void HttpPostExample::SendPostRequest(void)
{
	result r = E_SUCCESS;
	AppLog("initial result: %S", GetErrorMessage(r));

	String* pProxyAddr = null;
//	String hostAddr = L"http://localhost:port";
//	String uri = L"http://localhost:port/post/";

	String hostAddr = L"http://192.168.0.184:8080/beholder-master/post/";
	String uri = L"http://192.168.0.184:8080/beholder-master/post/";

	HttpHeader* pHeader= null;
	HttpTransaction* pHttpTransaction = null;

	// Create HTTP Session
	__pHttpSession = new HttpSession();
	r = __pHttpSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, pProxyAddr, hostAddr, null);
	AppLog("Session Construct result: %S", GetErrorMessage(r));

	// Open a new Http Transaction within the HttpSession
	pHttpTransaction = __pHttpSession->OpenTransactionN();
	AppLog("OpenTransactionN result: %S", GetErrorMessage(GetLastResult()));

	// Add Listener
	// OnTransactionCompleted() will be called when response is arrived
	r = pHttpTransaction->AddHttpTransactionListener(*this);
	AppLog("AddHttpTransactionListener result: %S", GetErrorMessage(r));

	// Get Http Request
	HttpRequest* pHttpRequest = pHttpTransaction->GetRequest();

	// Set HTTP Method
	r = pHttpRequest->SetMethod(NET_HTTP_METHOD_POST);
	AppLog("SetMethod result: %S", GetErrorMessage(r));

	// Set Uri
	r = pHttpRequest->SetUri(uri);
	AppLog("SetUri result: %S", GetErrorMessage(r));

	// Fill HTTP Header
	pHeader = pHttpRequest->GetHeader();
	AppLog("Get Header result: %S", GetErrorMessage(GetLastResult()));

	//Add a chunked field to header.
	r = pHeader->AddField(L"Transfer-Encoding", L"chunked");
	AppLog("AddField result: %S", GetErrorMessage(r));

	//Enable OnTransactionReadyToWrite()
	r = pHttpTransaction->EnableTransactionReadyToWrite();
	AppLog("EnableTransactionReadyToWrite result: %S", GetErrorMessage(r));
//
	//Set a Body to Request.
	ByteBuffer reqBody;
	const char* pBodyArray = "test1";
	int len = strlen(pBodyArray);
	reqBody.Construct(len);
	reqBody.SetArray((byte*)pBodyArray, 0, len);
	reqBody.Flip();
	r = pHttpRequest->WriteBody(reqBody);
	AppLog("Write Body result: %S", GetErrorMessage(r));

	// Submit a request with chunked mode
	r = pHttpTransaction->Submit();
	AppLog("Submit result: %S", GetErrorMessage(r));
}


void
HttpPostExample::OnTransactionReadyToRead(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
{
	AppLog("OnTransactionReadyToRead");
	ByteBuffer *pBody = null;
	HttpResponse* pHttpResponse = null;
	HttpHeader* pHttpHeader = null;

	// Get Response
	result r = E_SUCCESS;
	pHttpResponse = httpTransaction.GetResponse();
	AppLog("Getting response. Result is %S", GetErrorMessage(GetLastResult()));

	// Check the Status Code
//	if (pHttpResponse->GetHttpStatusCode() == HTTP_STATUS_OK)
//	{
////		// Get Http Headeregor
////		pHttpHeader = pHttpResponse->GetHeader();
////
////		// Read the body from the HttpResponse
////		pBody = pHttpResponse->ReadBodyN();
//
////		delete pBody;
//	}
}

void
HttpPostExample::OnTransactionReadyToWrite(HttpSession& httpSession, HttpTransaction& httpTransaction, int recommendedChunkSize)
{
	AppLog("OnTransactionReadyToWrite");
	HttpTransaction* pTransaction = &httpTransaction;
	HttpRequest* pRequest = pTransaction->GetRequest();
	ByteBuffer body;
	result r = E_SUCCESS;

	if(__isLastData)
	{
		//Set a last chunked flag.
		body.Construct(0);
		body.Flip();
		r =pRequest->WriteBody(body);
		AppLog("Write body #2 result: %S", GetErrorMessage(r));
	} else
	{
		//Set a chunked data.
		const char* pBodyArray = "test2";
		body.Construct(strlen(pBodyArray));
		r = body.SetArray((byte*)pBodyArray,0,strlen(pBodyArray));
		body.Flip();
		r =pRequest->WriteBody(body);
		AppLog("Write body #3 result: %S", GetErrorMessage(r));
		__isLastData = true;
	}
}

void
HttpPostExample::OnTransactionCompleted(HttpSession& httpSession, HttpTransaction& httpTransaction)
{
	AppLog("OnTransactionCompleted");
	//		delete &httpTransaction;
}
